using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class ShipmentDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetOne(int _Shipment_ID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Shipment_ID", _Shipment_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetOrderSales(out int ItemCount, int CustomerID, DateTime FromDate, DateTime ToDate, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetOrderSales", dbConn);
            dbCmd.Parameters.AddWithValue("@CustomerID", CustomerID);
            dbCmd.Parameters.AddWithValue("@FromDate", FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", ToDate);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetOrderSalesForReport(int CustomerID, DateTime FromDate, DateTime ToDate)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetOrderSalesForReport", dbConn);
            dbCmd.Parameters.AddWithValue("@CustomerID", CustomerID);
            dbCmd.Parameters.AddWithValue("@FromDate", FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", ToDate);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetListCustomerDebit(out int ItemCount, int CustomerID, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetListCustomerDebit", dbConn);
            dbCmd.Parameters.AddWithValue("@CustomerID", CustomerID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetCustomerDebit(int CustomerID)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetCustomerDebit", dbConn);
            dbCmd.Parameters.AddWithValue("@CustomerID", CustomerID);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetAllByCustomerForIncome(int CustomerID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetAllByCustomerForIncome", dbConn);
            dbCmd.Parameters.AddWithValue("@CustomerID", CustomerID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetList", dbConn);

            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static bool Delete(int _Shipment_ID)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Shipment_ID", _Shipment_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(ShipmentInfo _ShipmentInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@Shipment_Code", _ShipmentInfo.Shipment_Code);
            dbCmd.Parameters.AddWithValue("@Shipment_Date", _ShipmentInfo.Shipment_Date);
            dbCmd.Parameters.AddWithValue("@Shipment_Forms", _ShipmentInfo.Shipment_Forms);
            dbCmd.Parameters.AddWithValue("@Shipment_CustomerID", _ShipmentInfo.Shipment_CustomerID);
            dbCmd.Parameters.AddWithValue("@Shipment_Receive", _ShipmentInfo.Shipment_Receive);
            dbCmd.Parameters.AddWithValue("@Shipment_UsersID", _ShipmentInfo.Shipment_UsersID);
            dbCmd.Parameters.AddWithValue("@Shipment_TotalPrice", _ShipmentInfo.Shipment_TotalPrice);
            dbCmd.Parameters.AddWithValue("@Shipment_TotalDownPrice", _ShipmentInfo.Shipment_TotalDownPrice);
            dbCmd.Parameters.AddWithValue("@Shipment_TotalVAT", _ShipmentInfo.Shipment_TotalVAT);
            dbCmd.Parameters.AddWithValue("@Shipment_Total", _ShipmentInfo.Shipment_Total);
            dbCmd.Parameters.AddWithValue("@Shipment_Remaining", _ShipmentInfo.Shipment_Remaining);
            dbCmd.Parameters.AddWithValue("@Shipment_MoneySend", _ShipmentInfo.Shipment_MoneySend);


            dbCmd.Parameters.AddWithValue("@Shipment_Debit", _ShipmentInfo.Shipment_Debit);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static ShipmentInfo GetInfo(int _Shipment_ID)
        {
            ShipmentInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Shipment_ID", _Shipment_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ShipmentInfo();
                    retVal.Shipment_ID = Convert.ToInt32(dr["Shipment_ID"]);
                    retVal.Shipment_Code = Convert.ToString(dr["Shipment_Code"]);
                    retVal.Shipment_Date = Convert.ToDateTime(dr["Shipment_Date"]);
                    retVal.Shipment_Forms = Convert.ToString(dr["Shipment_Forms"]);
                    retVal.Shipment_CustomerID = Convert.ToInt32(dr["Shipment_CustomerID"]);
                    retVal.Shipment_Receive = Convert.ToString(dr["Shipment_Receive"]);
                    retVal.Shipment_UsersID = Convert.ToInt32(dr["Shipment_UsersID"]);
                    retVal.Shipment_Remaining = Convert.ToDecimal(dr["Shipment_Remaining"]);
                    if (dr["Shipment_Debit"] != DBNull.Value)
                        retVal.Shipment_Debit = Convert.ToDecimal(dr["Shipment_Debit"]);
                    else
                        retVal.Shipment_Debit = 0;
                    if (dr["Shipment_MoneySend"] != DBNull.Value)
                        retVal.Shipment_MoneySend = Convert.ToDecimal(dr["Shipment_MoneySend"]);
                    else
                        retVal.Shipment_MoneySend = 0;
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Using(int ShipmentID)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Shipment_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Shipment_ID", ShipmentID);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
       
        #endregion
    }
}
